package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.common.redis.service.RedisService;
import com.IntelligentCommunity.common.security.utils.SecurityUtils;
import com.IntelligentCommunity.community.domain.DistrictCount;
import com.IntelligentCommunity.community.domain.RealDistrict;
import com.IntelligentCommunity.community.domain.RealSecurityFacilities;
import com.IntelligentCommunity.community.domain.RealSecurityFacilitiesType;
import com.IntelligentCommunity.community.mapper.RealSecurityFacilitiesMapper;
import com.IntelligentCommunity.community.service.IRealSecurityFacilitiesService;
import com.IntelligentCommunity.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实有安防设施Service业务层处理
 *
 * @author IntelligentCommunity
 * @date 2024-07-21
 */
@Service
public class RealSecurityFacilitiesServiceImpl implements IRealSecurityFacilitiesService
{
    @Autowired
    private RealSecurityFacilitiesMapper realSecurityFacilitiesMapper;

    @Autowired
    private RedisService redisService;

    public static final String REDIS_KEY = "realSecurityFacilities";
    /**
     * 查询实有安防设施
     *
     * @param id 实有安防设施主键
     * @return 实有安防设施
     */
    @Override
    public RealSecurityFacilities selectRealSecurityFacilitiesById(Long id)
    {
        return realSecurityFacilitiesMapper.selectRealSecurityFacilitiesById(id);
    }

    /**
     * 查询实有安防设施列表
     *
     * @param realSecurityFacilities 实有安防设施
     * @return 实有安防设施
     */
    @Override
    public List<RealSecurityFacilities> selectRealSecurityFacilitiesList(RealSecurityFacilities realSecurityFacilities)
    {
        if(realSecurityFacilities.getFacilityType()!=null || realSecurityFacilities.getDistrictId()!=null || realSecurityFacilities.getInstalledDate()!=null || realSecurityFacilities.getMaintenanceDate()!=null || realSecurityFacilities.getCreateDate()!=null){
            return realSecurityFacilitiesMapper.selectRealSecurityFacilitiesList(realSecurityFacilities);
        }

        if(redisService.hasKey(REDIS_KEY)){
            List<RealSecurityFacilities> list = redisService.getCacheList(REDIS_KEY);
            return list;
        }

        List<RealSecurityFacilities> realSecurityFacilitiesList = realSecurityFacilitiesMapper.selectRealSecurityFacilitiesList(realSecurityFacilities);

        redisService.setCacheList(REDIS_KEY,realSecurityFacilitiesList);
        return realSecurityFacilitiesList;
    }

    /**
     * 新增实有安防设施
     *
     * @param realSecurityFacilities 实有安防设施
     * @return 结果
     */
    @Override
    public int insertRealSecurityFacilities(RealSecurityFacilities realSecurityFacilities)
    {
        redisService.deleteObject(REDIS_KEY);
        return realSecurityFacilitiesMapper.insertRealSecurityFacilities(realSecurityFacilities);
    }

    /**
     * 修改实有安防设施
     *
     * @param realSecurityFacilities 实有安防设施
     * @return 结果
     */
    @Override
    public int updateRealSecurityFacilities(RealSecurityFacilities realSecurityFacilities)
    {
        redisService.deleteObject(REDIS_KEY);
        return realSecurityFacilitiesMapper.updateRealSecurityFacilities(realSecurityFacilities);
    }

    /**
     * 批量删除实有安防设施
     *
     * @param ids 需要删除的实有安防设施主键
     * @return 结果
     */
    @Override
    public int deleteRealSecurityFacilitiesByIds(Long[] ids)
    {
        redisService.deleteObject(REDIS_KEY);
        return realSecurityFacilitiesMapper.deleteRealSecurityFacilitiesByIds(ids);
    }

    /**
     * 删除实有安防设施信息
     *
     * @param id 实有安防设施主键
     * @return 结果
     */
    @Override
    public int deleteRealSecurityFacilitiesById(Long id)
    {
        redisService.deleteObject(REDIS_KEY);
        return realSecurityFacilitiesMapper.deleteRealSecurityFacilitiesById(id);
    }

    @Override
    public List<RealSecurityFacilitiesType> getSecurityFacilitiesType() {
        return realSecurityFacilitiesMapper.getSecurityFacilitiesType();
    }

    @Override
    public RealSecurityFacilities getSecurityFacilitiesInfoById(Long id) {
        return realSecurityFacilitiesMapper.getSecurityFacilitiesInfoById(id);
    }

    @Override
    public DistrictCount getTypeCount(Long id) {
            return realSecurityFacilitiesMapper.getTypeCount(id);
    }

    @Override
    public List<RealSecurityFacilities> getGateByDictId(Long id) {
        return id == -1 ? null : realSecurityFacilitiesMapper.getGateByDictId(id);
    }

    /**
     * 功能：查询设备下拉框
     * @return
     */
    @Override
    public List<RealSecurityFacilities> getSecurityFacilitiesCount() {
        List<RealSecurityFacilities> list = realSecurityFacilitiesMapper.getSecurityFacilitiesCount();
        return list;
    }
}
